package com.secsbrain.development.web.controller;

import com.secsbrain.common.ErrorCodeEnum;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.common.utils.DateTimeUtils;
import com.secsbrain.development.biz.IDeployPlanService;
import com.secsbrain.development.biz.IProjectAppService;
import com.secsbrain.development.dal.model.DeployPlan;
import com.secsbrain.development.dal.model.ProjectApp;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.common.ResponseVo;
import com.secsbrain.development.client.model.TestResult;
import com.secsbrain.development.web.dto.PublishProjectDTO;
import com.secsbrain.development.web.dto.PublishProjectDTO.AppInfo;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

/**
 * 发布计划
 */
@RestController
@RequestMapping("api/project")
@ApiIgnore
public class PublishProjectController {
	
    @Resource
    private IDeployPlanService deployPlanService;
    
    @Resource
    IProjectAppService projectAppService;
    
    @RequestMapping(value = "/listNew", method = RequestMethod.POST)
    public ResponseResult projectListNew(@RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                                         @RequestParam(value = "length", defaultValue = "10") int length,
                                         @RequestParam("search") String projectName,
                                         @RequestParam("sortBy") String sortBy,
                                         @RequestParam(value = "descending") String descending) {
    	Page<DeployPlan> page=new Page<DeployPlan>(pageNo, length);
		DeployPlan deployPlan=new DeployPlan();
		deployPlan.setProjectName(projectName);
		deployPlan.setSortBy(sortBy);
		deployPlan.setDescending(Boolean.parseBoolean(descending));
		Page<DeployPlan> pageList=deployPlanService.queryDeployPlanList(page, deployPlan);
		for(DeployPlan deployPlanRes:pageList.getRecords()){
			deployPlanRes.setCreateTimeStr(DateTimeUtils.dateParseString(deployPlanRes.getCreateTime()));
			deployPlanRes.setUpdateTimeStr(DateTimeUtils.dateParseString(deployPlanRes.getUpdateTime()));
			deployPlanRes.setExecuteTimeStr(DateTimeUtils.dateParseString(deployPlanRes.getExecuteTime()));
			
			ProjectApp projectAppParam=new ProjectApp();
	        projectAppParam.setProjectId(deployPlanRes.getId());
	        int count=projectAppService.selectCount(projectAppParam);
	        if(count>0){
	        	deployPlanRes.setStatus("PASS");
	        }
	        
		}
        return new ResponseResult<>(pageList);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseResult createProject(@RequestBody PublishProjectDTO param) {
    	deployPlanService.saveOrUpdateDeployPlan(param);
        return new ResponseResult<>();
    }

    @RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
    public ResponseResult info(@PathVariable("id") long id) {
        PublishProjectDTO dto = new PublishProjectDTO();
        DeployPlan deployPlan=deployPlanService.selectById(id);
        if (deployPlan == null) {
            return new ResponseResult<>(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),"无效的发布计划ID");
        }
        
        dto.setId(deployPlan.getId());
        dto.setProjectName(deployPlan.getProjectName()+"_"+deployPlan.getBuildName());
        dto.setProjectDesc(deployPlan.getBuildDes());
        
        ProjectApp projectAppParam=new ProjectApp();
        projectAppParam.setProjectId(id);
        List<ProjectApp> projectAppList=projectAppService.selectList(projectAppParam);
        List<AppInfo> appInfoList = new ArrayList<>();
        for(ProjectApp projectApp:projectAppList){
        	AppInfo appInfo=new AppInfo();
        	appInfo.setAppName(projectApp.getAppName());
            appInfo.setBranch(projectApp.getAppBranch());
            appInfo.setDisconfVersion(projectApp.getDisconfVersion());
            appInfo.setId(projectApp.getAppId());
            appInfoList.add(appInfo);
        }
        dto.setAppList(appInfoList);
        return new ResponseResult<>(dto);
    }
    
    /**
   	 * @api {GET} /api/project/testResult/{id}  查看测试报告
   	 * 
   	 * @apiGroup PROJECT
   	 * @apiParam {Number{11}} id   发布计划唯一标识
   	 * 
   	 */
    @RequestMapping(value = "/testResult/{id}", method = RequestMethod.GET)
    public ResponseResult testResult(@PathVariable("id") Long id) {
		ResponseVo<TestResult> responseVo=projectAppService.getTestResult(null,id);
		
    	if(!responseVo.isSuccess()){
            return new ResponseResult<>(responseVo.getErrorCode(),responseVo.getMsg());
		}
		if(responseVo.getData()!=null){
			TestResult testResult= responseVo.getData();
			if(testResult.getCode()==0){
                ResponseResult responseResult=new ResponseResult<>(ErrorCodeEnum.ERROR_COMMON_SYSTEM.getCode(),"不满足发布条件");
                responseResult.setData(testResult.getDetailList());
	    		return responseResult;
			}
            return new ResponseResult<>(testResult.getDetailList());
		}
        return new ResponseResult<>();
    }

}
